﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data;
using DevExpress.XtraEditors.Controls;

namespace SoufunData
{
    public partial class ZProjectContractForm : DevExpress.XtraEditors.XtraForm
    {

        public ZProjectContractForm()
        {
            InitializeComponent();
            dateEdit_startSell.DateTime = DateTime.Now.AddDays(-60);
            dateEdit_endSell.DateTime = DateTime.Now;
            _dtResult = new DataTable();
            _dtResult.Columns.Add("名次");
            _dtResult.Columns.Add("销售套数");
            _dtResult.Columns.Add("销售面积");
            _dtResult.Columns.Add("销售金额");
            _dtResult.Columns.Add("销售均价");

            for (int i = 1; i <= TopCount; i++)
            {
                DataRow row = _dtResult.NewRow();
                row["名次"] = i;
                _dtResult.Rows.Add(row);
            }

            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;
        }
         
        private DataTable _dtResult;
        private readonly SqlClass _sqlClass = new SqlClass();
        private const int TopCount = 30;

        private void AdvertisingForm_Load(object sender, EventArgs e)
        {
          
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }

        
         
        private void LoatData()
        {
            
            StringBuilder strWhere = new StringBuilder();

            strWhere.Append("SellTime between '");
            strWhere.Append(dateEdit_startSell.DateTime);
            strWhere.Append("' and '");
            strWhere.Append(dateEdit_endSell.DateTime);
            strWhere.Append("' ");

            if (imgCombox_xzq.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("AdministratiVeregion='");
                strWhere.Append(imgCombox_xzq.Properties.Items[
                    imgCombox_xzq.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombox_board.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("Board='");
                strWhere.Append(imgCombox_board.Properties.Items[
                    imgCombox_board.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombo_houseType.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("HouseType like '%");
                strWhere.Append(imgCombo_houseType.Properties.Items[
                    imgCombo_houseType.SelectedIndex].Description);
                strWhere.Append("%' ");
            }
            if (imgCombo_buildShape.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("FloorCount='");
                strWhere.Append(imgCombo_buildShape.Properties.Items[
                    imgCombo_buildShape.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombo_PropertyType.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("PropertyType='");
                strWhere.Append(imgCombo_PropertyType.Properties.Items[
                    imgCombo_PropertyType.SelectedIndex].Description);
                strWhere.Append("' ");
            }
            if (imgCombo_isBzf.SelectedIndex != 0)
            {
                strWhere.Append(" and ");
                strWhere.Append("IsBaoZhangFang='");
                strWhere.Append(imgCombo_isBzf.Properties.Items[
                    imgCombo_isBzf.SelectedIndex].Description);
                strWhere.Append("' ");
            }

            //if (imgCombox_wy.SelectedIndex!=0)
            //{
            //    if (strWhere.ToString() != "")
            //    {
            //        strWhere.Append(" and ");
            //    }
            //    strWhere.Append("PropertyType='");
            //    strWhere.Append(imgCombox_wy.Properties.Items[
            //        imgCombox_wy.SelectedIndex].Description);
            //    strWhere.Append("' ");
            //}
            //if (imgCombox_media.SelectedIndex!=0)
            //{
            //    if (strWhere.ToString() != "")
            //    {
            //        strWhere.Append(" and ");
            //    }
            //    strWhere.Append(" PublicMedia='");
            //    strWhere.Append(imgCombox_media.Properties.Items[
            //        imgCombox_media.SelectedIndex].Description);
            //    strWhere.Append("' ");
            //}

            if (textEdit_proName.Text.Trim() != "")
            {                 
                strWhere.Append(" and ");
                strWhere.Append(radioGroup1.Properties.Items[
                    radioGroup1.SelectedIndex].Value);    
                strWhere.Append(" like '%");
                strWhere.Append(textEdit_proName.Text);
                strWhere.Append("%'");
            }
            try
            {
                DataSet ds = _sqlClass.ZProjectContractGetList(strWhere.ToString());
                if (ds == null || ds.Tables.Count == 0)
                {
                    XtraMessageBox.Show("数据库查询出错！请检查数据库连接是否正常！");
                    return;
                }
                GetResultData(ds.Tables[0]);
                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl2.DataSource = ds.Tables[0];
                    gridView2.GroupPanelText = "共记"
                        + ds.Tables[0].Rows.Count + "条数据";
                }
                ));
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } 
        }

        private void GetResultData(DataTable dt)
        {
            DataView dv = dt.DefaultView;
            //销售套数排序
            dv.Sort = "SellCount desc";
            DataTable dtTemp = dv.ToTable();
            for (int i = 0; i < dtTemp.Rows.Count ; i++)
            {
                if (i<TopCount)
                {
                    string proName = dtTemp.Rows[i]["ProjectName"].ToString();
                    string houseType = dtTemp.Rows[i]["HouseType"].ToString();
                    string strSellCount = dtTemp.Rows[i]["SellCount"].ToString();
                    _dtResult.Rows[i]["销售套数"] = proName + "  " + houseType +
                        " (" + strSellCount + ")";
                }
            }
            //销售面积
            dv.Sort = "SellArea desc";
            dtTemp.Clear();
            dtTemp = dv.ToTable();
            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                if (i < TopCount)
                {
                    string proName = dtTemp.Rows[i]["ProjectName"].ToString();
                    string houseType = dtTemp.Rows[i]["HouseType"].ToString();
                    string strSellArea = dtTemp.Rows[i]["SellArea"].ToString();
                    _dtResult.Rows[i]["销售面积"] = proName + "  "+ houseType+
                       " (" + strSellArea + ")";
                }
            }
            //销售金额
            dv.Sort = "SellMoney desc";
            dtTemp.Clear();
            dtTemp = dv.ToTable();
            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                if (i < TopCount)
                {
                    string proName = dtTemp.Rows[i]["ProjectName"].ToString();
                    string houseType = dtTemp.Rows[i]["HouseType"].ToString();
                    string strSellMoney = dtTemp.Rows[i]["SellMoney"].ToString();
                    _dtResult.Rows[i]["销售金额"] = proName + "  " + houseType +
                         " (" + strSellMoney + ")";
                }
            }
            //销售均价
            dv.Sort = "SellFold desc";
            dtTemp.Clear();
            dtTemp = dv.ToTable();
            for (int i = 0; i < dtTemp.Rows.Count; i++)
            {
                if (i < TopCount)
                {
                    string proName = dtTemp.Rows[i]["ProjectName"].ToString();
                    string houseType = dtTemp.Rows[i]["HouseType"].ToString();
                    string strSellFold = dtTemp.Rows[i]["SellFold"].ToString();
                    _dtResult.Rows[i]["销售均价"] = proName + "  " + houseType +
                         " (" + strSellFold + ")";
                }
            }
             
            BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = _dtResult;
                    gridView1.GroupPanelText = "排行榜-Top30";
                }));
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            gridView2.GroupPanelText = "正在查询...";
            Thread t = new Thread(LoatData) { IsBackground = true };
            t.Start();
        }
         
        private void textEdit_proName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
               btn_select_Click(null,null);
            }
        }
  
        private void btn_toExcel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "项目成交数据";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView2.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
 

        private void btn_importResultExcel_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            string fileName = "项目成交数据排行榜";
            saveFileDialog1.FileName = fileName;
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }
 

        private void textEdit_Leave(object sender, EventArgs e)
        {
            TextEdit textEdit = sender as TextEdit;
            if (textEdit.Text == "0" || textEdit.Text.Trim() == "")
            {
                textEdit.Text = "不限制";    
            }            
        }
 
    }
}